﻿namespace OP10.ViewModels
{
    public class ST04YAxisRightServoViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;
        public ST04YAxisRightServoViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;



            InitServoParam();

            //开启定时器任务
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(200);
            dispatcherTimer.Tick += OnTick;
            dispatcherTimer.Start();
        }

        #region 私有方法
        private void OnTick(object? sender, EventArgs e)
        {
            //界面UI刷新
            bError = YAxisRightServo.ServoPLCToPC_Global.bAxisAlarm;
            bLimit = YAxisRightServo.ServoPLCToPC_Global.bLimmitAlarm;
            bManualLock = YAxisRightServo.ServoPLCToPC_Global.bManualLock;
            bAutoLock = YAxisRightServo.ServoPLCToPC_Global.bAutoLock;
            bPowerOK = YAxisRightServo.ServoPLCToPC_Global.bPowerOK;
            bHomeCmp = YAxisRightServo.ServoPLCToPC_Global.bHomeCmp;
            bForwardMoving = YAxisRightServo.ServoPLCToPC_Global.bForwardMoving;
            bReverseMoving = YAxisRightServo.ServoPLCToPC_Global.bReverseMoving;
            bManualMoving = YAxisRightServo.ServoPLCToPC_Global.bManualMoving;
            ServoParam.CurrentPos = YAxisRightServo.ServoPLCToPC_Global.nActualPosition;
            ServoParam.CurrentNum = YAxisRightServo.ServoPLCToPC_Global.nAutoIndex;

        }

        /// <summary>
        /// 初始化伺服参数
        /// </summary>
        /// 
        private void InitServoParam()
        {
            ServoParam.CurrentPos = YAxisRightServo.ServoPLCToPC_Global.nActualPosition;
            ServoParam.CurrentNum = YAxisRightServo.ServoPLCToPC_Global.nAutoIndex;
            ServoParam.Acceleration = YAxisRightServoParam_Global.nAcc;
            ServoParam.Deceleration = YAxisRightServoParam_Global.nDec;
            ServoParam.JogSpeedPercent = YAxisRightServoParam_Global.Override;
            ServoParam.JogSpeed = YAxisRightServoParam_Global.nJogSpeed;
            ServoParam.Servo0Pos = YAxisRightServoParam_Global.nHomePos;
            ServoParam.Servo0Speed = YAxisRightServoParam_Global.nHomeSpeed;


            ServoParam.Servo1Pos = YAxisRightServoParam_Global.aWork[0].aWorkPos;
            ServoParam.Servo2Pos = YAxisRightServoParam_Global.aWork[1].aWorkPos;
            ServoParam.Servo3Pos = YAxisRightServoParam_Global.aWork[2].aWorkPos;
            ServoParam.Servo4Pos = YAxisRightServoParam_Global.aWork[3].aWorkPos;
            ServoParam.Servo5Pos = YAxisRightServoParam_Global.aWork[4].aWorkPos;
            ServoParam.Servo6Pos = YAxisRightServoParam_Global.aWork[5].aWorkPos;
            ServoParam.Servo7Pos = YAxisRightServoParam_Global.aWork[6].aWorkPos;
            ServoParam.Servo8Pos = YAxisRightServoParam_Global.aWork[7].aWorkPos;
            ServoParam.Servo9Pos = YAxisRightServoParam_Global.aWork[8].aWorkPos;
            ServoParam.Servo10Pos = YAxisRightServoParam_Global.aWork[9].aWorkPos;


            ServoParam.Servo1Speed = YAxisRightServoParam_Global.aWork[0].aWorkSpeed;
            ServoParam.Servo2Speed = YAxisRightServoParam_Global.aWork[1].aWorkSpeed;
            ServoParam.Servo3Speed = YAxisRightServoParam_Global.aWork[2].aWorkSpeed;
            ServoParam.Servo4Speed = YAxisRightServoParam_Global.aWork[3].aWorkSpeed;
            ServoParam.Servo5Speed = YAxisRightServoParam_Global.aWork[4].aWorkSpeed;
            ServoParam.Servo6Speed = YAxisRightServoParam_Global.aWork[5].aWorkSpeed;
            ServoParam.Servo7Speed = YAxisRightServoParam_Global.aWork[6].aWorkSpeed;
            ServoParam.Servo8Speed = YAxisRightServoParam_Global.aWork[7].aWorkSpeed;
            ServoParam.Servo9Speed = YAxisRightServoParam_Global.aWork[8].aWorkSpeed;
            ServoParam.Servo10Speed = YAxisRightServoParam_Global.aWork[9].aWorkSpeed;

        }
        #endregion

        #region 事件


        private DelegateCommand _GoBackCommand;

        public DelegateCommand GoBackCommand =>
            _GoBackCommand ?? (_GoBackCommand = new DelegateCommand(ExecuteGoBackCommand));

        void ExecuteGoBackCommand()
        {
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST04DeviceManagerView);
        }


        private DelegateCommand _BtnHomeMouseDownCmd;
        public DelegateCommand BtnHomeMouseDownCmd =>
            _BtnHomeMouseDownCmd ?? (_BtnHomeMouseDownCmd = new DelegateCommand(ExecuteBtnHomeMouseDownCmd));

        void ExecuteBtnHomeMouseDownCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bHomeBT = true;
        }

        private DelegateCommand _BtnHomeMouseUpCmd;
        public DelegateCommand BtnHomeMouseUpCmd =>
            _BtnHomeMouseUpCmd ?? (_BtnHomeMouseUpCmd = new DelegateCommand(ExecuteBtnHomeMouseUpCmd));

        void ExecuteBtnHomeMouseUpCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bHomeBT = false;

        }


        private DelegateCommand _BtnPowerMouseDownCmd;
        public DelegateCommand BtnPowerMouseDownCmd =>
            _BtnPowerMouseDownCmd ?? (_BtnPowerMouseDownCmd = new DelegateCommand(ExecuteBtnPowerMouseDownCmd));

        void ExecuteBtnPowerMouseDownCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bReleaseBT = true;
        }


        private DelegateCommand _BtnPowerMouseUpCmd;
        public DelegateCommand BtnPowerMouseUpCmd =>
            _BtnPowerMouseUpCmd ?? (_BtnPowerMouseUpCmd = new DelegateCommand(ExecuteBtnPowerMouseUpCmd));

        void ExecuteBtnPowerMouseUpCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bReleaseBT = false;
        }

        private DelegateCommand _BtnLeftMouseDownCmd;
        public DelegateCommand BtnLeftMouseDownCmd =>
            _BtnLeftMouseDownCmd ?? (_BtnLeftMouseDownCmd = new DelegateCommand(ExecuteBtnLeftMouseDownCmd));

        void ExecuteBtnLeftMouseDownCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bReverseBT = true;
        }


        private DelegateCommand _BtnLeftMouseUpCmd;
        public DelegateCommand BtnLeftMouseUpCmd =>
            _BtnLeftMouseUpCmd ?? (_BtnLeftMouseUpCmd = new DelegateCommand(ExecuteBtnLeftMouseUpCmd));

        void ExecuteBtnLeftMouseUpCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bReverseBT = false;
        }

        private DelegateCommand _BtnRightMouseDownCmd;
        public DelegateCommand BtnRightMouseDownCmd =>
            _BtnRightMouseDownCmd ?? (_BtnRightMouseDownCmd = new DelegateCommand(ExecuteBtnRightMouseDownCmd));

        void ExecuteBtnRightMouseDownCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bForwardBT = true;
        }


        private DelegateCommand _BtnRightMouseUpCmd;
        public DelegateCommand BtnRightMouseUpCmd =>
            _BtnRightMouseUpCmd ?? (_BtnRightMouseUpCmd = new DelegateCommand(ExecuteBtnRightMouseUpCmd));

        void ExecuteBtnRightMouseUpCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bForwardBT = false;
        }



        private DelegateCommand _BtnGoLocateMouseDownCmd;
        public DelegateCommand BtnGoLocateMouseDownCmd =>
            _BtnGoLocateMouseDownCmd ?? (_BtnGoLocateMouseDownCmd = new DelegateCommand(ExecuteBtnGoLocateMouseDownCmd));

        void ExecuteBtnGoLocateMouseDownCmd()
        {

            YAxisRightServo.servoPCToPLC_Global.nHmiIndex = Number;
            YAxisRightServo.servoPCToPLC_Global.bManualMoveBT = true;
        }


        private DelegateCommand _BtnGoLocateMouseUpCmd;
        public DelegateCommand BtnGoLocateMouseUpCmd =>
            _BtnGoLocateMouseUpCmd ?? (_BtnGoLocateMouseUpCmd = new DelegateCommand(ExecuteBtnGoLocateMouseUpCmd));

        void ExecuteBtnGoLocateMouseUpCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bManualMoveBT = false;

        }

        private DelegateCommand _BtnBackAutoLocateMouseDownCmd;
        public DelegateCommand BtnBackAutoLocateMouseDownCmd =>
            _BtnBackAutoLocateMouseDownCmd ?? (_BtnBackAutoLocateMouseDownCmd = new DelegateCommand(ExecuteBtnBackAutoLocateMouseDownCmd));

        void ExecuteBtnBackAutoLocateMouseDownCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bBackAutoPosBT = true;
        }


        private DelegateCommand _BtnBackAutoLocateMouseUpCmd;
        public DelegateCommand BtnBackAutoLocateMouseUpCmd =>
            _BtnBackAutoLocateMouseUpCmd ?? (_BtnBackAutoLocateMouseUpCmd = new DelegateCommand(ExecuteBtnBackAutoLocateMouseUpCmd));

        void ExecuteBtnBackAutoLocateMouseUpCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bBackAutoPosBT = false;

        }




        private DelegateCommand<string> _TextChangeCommand;
        public DelegateCommand<string> TextChangeCommand =>
            _TextChangeCommand ?? (_TextChangeCommand = new DelegateCommand<string>(ExecuteTextChangeCommand));

        void ExecuteTextChangeCommand(string p)
        {
            switch (p)
            {
                case "加速度":
                    YAxisRightServoParam_Global.nAcc = ServoParam.Acceleration;
                    break;
                case "减速度":
                    YAxisRightServoParam_Global.nDec = ServoParam.Deceleration;
                    break;
                case "JOG速度百分比":
                    YAxisRightServoParam_Global.Override = ServoParam.JogSpeedPercent;
                    break;
                case "原点位置":
                    YAxisRightServoParam_Global.nHomePos = ServoParam.Servo0Pos;
                    break;
                case "原点速度":
                    YAxisRightServoParam_Global.nHomeSpeed = ServoParam.Servo0Speed;
                    break;
                case "JOG速度":
                    YAxisRightServoParam_Global.nJogSpeed = ServoParam.JogSpeed;
                    break;

                case "位置1":
                    YAxisRightServoParam_Global.aWork[0].aWorkPos = ServoParam.Servo1Pos;
                    break;
                case "位置1速度":
                    YAxisRightServoParam_Global.aWork[0].aWorkSpeed = ServoParam.Servo1Speed;
                    break;
                case "位置2":
                    YAxisRightServoParam_Global.aWork[1].aWorkPos = ServoParam.Servo2Pos;
                    break;
                case "位置2速度":
                    YAxisRightServoParam_Global.aWork[1].aWorkSpeed = ServoParam.Servo2Speed;

                    break;
                case "位置3":
                    YAxisRightServoParam_Global.aWork[2].aWorkPos = ServoParam.Servo3Pos;
                    break;
                case "位置3速度":
                    YAxisRightServoParam_Global.aWork[2].aWorkSpeed = ServoParam.Servo3Speed;

                    break;
                case "位置4":
                    YAxisRightServoParam_Global.aWork[3].aWorkPos = ServoParam.Servo4Pos;
                    break;
                case "位置4速度":
                    YAxisRightServoParam_Global.aWork[3].aWorkSpeed = ServoParam.Servo4Speed;

                    break;
                case "位置5":
                    YAxisRightServoParam_Global.aWork[4].aWorkPos = ServoParam.Servo5Pos;
                    break;
                case "位置5速度":
                    YAxisRightServoParam_Global.aWork[4].aWorkSpeed = ServoParam.Servo5Speed;

                    break;
                case "位置6":
                    YAxisRightServoParam_Global.aWork[5].aWorkPos = ServoParam.Servo6Pos;
                    break;
                case "位置6速度":
                    YAxisRightServoParam_Global.aWork[5].aWorkSpeed = ServoParam.Servo6Speed;

                    break;
                case "位置7":
                    YAxisRightServoParam_Global.aWork[6].aWorkPos = ServoParam.Servo7Pos;
                    break;
                case "位置7速度":
                    YAxisRightServoParam_Global.aWork[6].aWorkSpeed = ServoParam.Servo7Speed;

                    break;
                case "位置8":
                    YAxisRightServoParam_Global.aWork[7].aWorkPos = ServoParam.Servo8Pos;
                    break;
                case "位置8速度":
                    YAxisRightServoParam_Global.aWork[7].aWorkSpeed = ServoParam.Servo8Speed;

                    break;
                case "位置9":
                    YAxisRightServoParam_Global.aWork[8].aWorkPos = ServoParam.Servo9Pos;
                    break;
                case "位置9速度":
                    YAxisRightServoParam_Global.aWork[8].aWorkSpeed = ServoParam.Servo9Speed;

                    break;
                case "位置10":
                    YAxisRightServoParam_Global.aWork[9].aWorkPos = ServoParam.Servo10Pos;
                    break;
                case "位置10速度":
                    YAxisRightServoParam_Global.aWork[9].aWorkSpeed = ServoParam.Servo10Speed;

                    break;



                default:
                    break;
            }
        }


        private DelegateCommand _NavigateLeftCmd;
        public DelegateCommand NavigateLeftCmd =>
            _NavigateLeftCmd ?? (_NavigateLeftCmd = new DelegateCommand(ExecuteNavigateLeftCmd));

        void ExecuteNavigateLeftCmd()
        {
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST04YAxisLeftServoView);
        }
        #endregion

        #region 属性

        private ServoModel _ServoParam;
        public ServoModel ServoParam
        {
            get { return _ServoParam ?? (_ServoParam = new ServoModel()); }
            set { SetProperty(ref _ServoParam, value); }
        }

        /// <summary>
        /// 错误
        /// </summary>
        private bool _bError;
        public bool bError
        {
            get { return _bError; }
            set { SetProperty(ref _bError, value); }
        }

        /// <summary>
        /// 极限
        /// </summary>
        private bool _bLimit;
        public bool bLimit
        {
            get { return _bLimit; }
            set { SetProperty(ref _bLimit, value); }
        }
        /// <summary>
        /// 编号
        /// </summary>
        private int _Number;
        public int Number
        {
            get { return _Number; }
            set { SetProperty(ref _Number, value); }
        }


        private bool _bHomeCmp;
        public bool bHomeCmp
        {
            get { return _bHomeCmp; }
            set { SetProperty(ref _bHomeCmp, value); }
        }

        private bool _bPowerOK;
        public bool bPowerOK
        {
            get { return _bPowerOK; }
            set { SetProperty(ref _bPowerOK, value); }
        }

        private bool _bForwardMoving;
        public bool bForwardMoving
        {
            get { return _bForwardMoving; }
            set { SetProperty(ref _bForwardMoving, value); }
        }


        private bool _bReverseMoving;
        public bool bReverseMoving
        {
            get { return _bReverseMoving; }
            set { SetProperty(ref _bReverseMoving, value); }
        }


        private bool _bNotInAutoPos;
        public bool bNotInAutoPos
        {
            get { return _bNotInAutoPos; }
            set { SetProperty(ref _bNotInAutoPos, value); }
        }


        private bool _bAutoLock;
        public bool bAutoLock
        {
            get { return _bAutoLock; }
            set { SetProperty(ref _bAutoLock, value); }
        }

        private bool _bManualMoving;
        public bool bManualMoving
        {
            get { return _bManualMoving; }
            set { SetProperty(ref _bManualMoving, value); }
        }

        private bool _bManualLock;
        public bool bManualLock
        {
            get { return _bManualLock; }
            set { SetProperty(ref _bManualLock, value); }
        }
        #endregion
    }
}
